package com.zhuchaooasystem.ui.login;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhuchaooasystem.R;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.presenter.login.LoginContract;
import com.zhuchaooasystem.presenter.login.LoginPresenter;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.SharedprefrenceUtils;
import com.zhuchaooasystem.widget.StateButton;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.act_login_edit_mobile)
    EditText edit_mobile;
    @BindView(R.id.act_login_edit_password)
    EditText edit_password;
    @BindView(R.id.act_login_txt_forget_password)
    TextView txt_forget_password;
    @BindView(R.id.act_login_state_btn_login)
    StateButton state_btn_login;
    @BindView(R.id.act_login_txt_register)
    TextView txt_register;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter getPresenter() {
        return new LoginPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
    }

    @Override
    protected void initView() {
        if (!TextUtils.isEmpty(SharedprefrenceUtils.getUsername(mContext))) {
            edit_mobile.setText(SharedprefrenceUtils.getUsername(mContext));
        }
        //设置EditView焦点获取
        edit_mobile.setFocusable(false);

        edit_mobile.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                edit_mobile.setFocusableInTouchMode(true);

                return false;
            }
        });

        edit_password.setFocusable(false);

        edit_password.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                edit_password.setFocusableInTouchMode(true);

                return false;
            }
        });
    }

    @Override
    protected void initDataAndLoadData() {
    }

    @OnClick({R.id.act_login_state_btn_login, R.id.act_login_txt_register})
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.act_login_state_btn_login:
                //如果是安卓6.0的系统  读取电话的权限
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    //请求权限
                    requestPermission(new String[]{Manifest.permission.READ_PHONE_STATE}, Key.READ_PHONE_STATE);
                } else {
                    mPresenter.login();
                }

                break;
            //注册
            case R.id.act_login_txt_register:
                AppUtils.jump(LoginActivity.this, RegisterActivity.class);
                break;
        }
    }

    @Override
    public String getMobile() {
        return edit_mobile.getText().toString();
    }

    @Override
    public String getPassword() {
        return edit_password.getText().toString();
    }

    @Override
    public void setLoginClick(boolean canClick) {
        if (canClick) {
            state_btn_login.setClickable(true);
        } else {
            state_btn_login.setClickable(false);
        }
    }

    /**
     * 权限成功回调函数
     *
     * @param requestCode
     */
    @Override
    public void permissionSuccess(int requestCode) {
        super.permissionSuccess(requestCode);

        switch (requestCode) {
            case Key.READ_PHONE_STATE:

                mPresenter.login();

                break;
        }
    }

    /**
     * 权限失败回调函数
     *
     * @param requestCode
     */
    @Override
    public void permissionFail(int requestCode) {
        super.permissionFail(requestCode);
        Toast.makeText(mContext, "获取权限失败", Toast.LENGTH_SHORT).show();
    }

}
